package com.ysxsoft.wdg.ui.activity.home;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.home.JxGoodsAdapter;
import com.ysxsoft.wdg.adapter.home.ZyGoodsAdapter;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.response.HomeProductResponse;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.util.AlibcUtils;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 淘宝客搜索结果
 */
public class TbSearchListActivity extends BaseActivity {

    public static void start(Context context, String keyword) {
        Intent intent = new Intent(context, TbSearchListActivity.class);
        intent.putExtra("keyword", keyword);
        context.startActivity(intent);
    }

    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_content)
    EditText titleContent;
    @BindView(R.id.tv0)
    TextView tv0;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.tv3)
    TextView tv3;
    @BindView(R.id.iv3)
    ImageView iv3;
    @BindView(R.id.recyclerview2)
    RecyclerView recyclerview2;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.multStatusView)
    MultipleStatusView multStatusView;

    private TextView[] textViews;

    private JxGoodsAdapter adapter;
    private String keyword;
    private int page=1;
    private int type=0;//综合0，销量1，最新2，到手价倒序3， 到手价正序4

    @Override
    protected int getLayoutId() {
        return R.layout.activity_tb_search_list;
    }

    @Override
    protected void initData() {
        initStatusBar(topView, false);
        titleContent.setText("");

        keyword=getIntent().getStringExtra("keyword");
        textViews = new TextView[]{tv0, tv1, tv2, tv3};
        setSelect(0);

        recyclerview2.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        adapter = new JxGoodsAdapter(R.layout.item_goods);
        recyclerview2.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                HomeProductResponse.DataBean item = (HomeProductResponse.DataBean) adapter.getItem(position);
                AlibcUtils.showDetail(TbSearchListActivity.this, item.getNum_iid(), SharePrefUtils.getUserId());
            }
        });
        getProductList();
    }

    @Override
    protected void setListener() {
            refreshLayout.setOnRefreshListener(new OnRefreshListener() {
                @Override
                public void onRefresh(RefreshLayout refreshlayout) {
                    page = 1;
                    getProductList();
                }
            });
            refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
                @Override
                public void onLoadmore(RefreshLayout refreshlayout) {
                    page++;
                    getProductList();
                }
            });
    }

    @OnClick({R.id.title_finish, R.id.tv0, R.id.tv1, R.id.tv2, R.id.tv3,R.id.title_tv_r})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_finish:
                finish();
                break;
            case R.id.tv0:
                type = 0;
                setSelect(0);
                break;
            case R.id.tv1:
                type = 1;
                setSelect(1);
                break;
            case R.id.tv2:
                type = 2;
                setSelect(2);
                break;
            case R.id.tv3:
                type = (type == 3) ? 4 : 3;
                setSelect(3);
                iv3.setImageResource((type == 3) ? R.mipmap.xu_down : R.mipmap.xu_up);
                break;
            case R.id.title_tv_r:
                page=1;
                getProductList();
                break;
        }
    }

    private void setSelect(int position) {
        for (int i = 0; i < textViews.length; i++) {
            textViews[i].setSelected(i == position);
        }
        page=1;
        getProductList();
    }

    /**
     * 获取列表
     */
    /**
     * 获取购物精选数据
     */
    private void getProductList() {
        if (page == 1) {
            multStatusView.showLoading();
        }
        if(!"".equals(titleContent.getText().toString())){
            keyword=titleContent.getText().toString();
        }
        OkHttpUtils.post()
                .url(UrlHost.HOME_PRODUCT)
                .addParams("uid",  SharePrefUtils.getUserId())
                .addParams("page", page + "")
                .addParams("type", "" + type)//	综合0，销量1，最新2，到手价倒序3，到手价正序4
                .addParams("keyword", keyword)
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                    multStatusView.showError();
                    reset();
                }
            }

            @Override
            public void onResponse(String response, int id) {
                reset();
                if (response != null) {
                    HomeProductResponse homeProductResponse = JsonUtils.parseByGson(response, HomeProductResponse.class);
                    if (homeProductResponse == null) {
                        return;
                    }
                    if (CallbackCode.SUCCESS.equals(homeProductResponse.getCode())) {
                        List<HomeProductResponse.DataBean> list = homeProductResponse.getData();
                        if (page == 1) {
                            multStatusView.showContent();
                            if (list != null && !list.isEmpty()) {
                            } else {
                                multStatusView.showEmpty();
                            }
                            adapter.setNewData(list);
                        } else {
                            adapter.addData(list);
                        }
                    }else{
                        if(page==1){
                            multStatusView.showEmpty();
                        }
                    }
                } else {
                    multStatusView.showError();
                }
            }
        });
    }

    private void reset() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh(true);
        }
        refreshLayout.finishLoadmore();
    }
}
